package org.soc.timesheet.domain;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "EMAILS")
@Configurable
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "EMAILS")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "idUsers" })
@RooJson
@JsonIgnoreProperties({"idUsers"})
public class Emails {

	@ManyToOne
    @JoinColumn(name = "ID_USERS", referencedColumnName = "ID", nullable = false)
    private Users idUsers;

	@Column(name = "EMAIL", length = 500)
    @NotNull
    private String email;

	public Users getIdUsers() {
        return idUsers;
    }

	public void setIdUsers(Users idUsers) {
        this.idUsers = idUsers;
    }

	public String getEmail() {
        return email;
    }

	public void setEmail(String email) {
        this.email = email;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_EMAIL")
    private Integer idEmail;

	public Integer getIdEmail() {
        return this.idEmail;
    }

	public void setIdEmail(Integer id) {
        this.idEmail = id;
    }

	public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("idUsers").toString();
    }

	public String toJson() {
        return new JSONSerializer()
        .exclude("*.class").serialize(this);
    }

	public String toJson(String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(this);
    }

	public static Emails fromJsonToEmails(String json) {
        return new JSONDeserializer<Emails>()
        .use(null, Emails.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Emails> collection) {
        return new JSONSerializer()
        .exclude("*.class").serialize(collection);
    }

	public static String toJsonArray(Collection<Emails> collection, String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(collection);
    }

	public static Collection<Emails> fromJsonArrayToEmailses(String json) {
        return new JSONDeserializer<List<Emails>>()
        .use("values", Emails.class).deserialize(json);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("");

	public static final EntityManager entityManager() {
        EntityManager em = new Emails().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countEmailses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Emails o", Long.class).getSingleResult();
    }

	public static List<Emails> findAllEmailses() {
        return entityManager().createQuery("SELECT o FROM Emails o", Emails.class).getResultList();
    }

	public static List<Emails> findAllEmailses(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Emails o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Emails.class).getResultList();
    }

	public static Emails findEmails(Integer idEmail) {
        if (idEmail == null) return null;
        return entityManager().find(Emails.class, idEmail);
    }

	public static List<Emails> findEmailsEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Emails o", Emails.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public static List<Emails> findEmailsEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM Emails o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, Emails.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Emails attached = Emails.findEmails(this.idEmail);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Emails merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Emails merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
